package cn.csc.module.shop.controller.app.user;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.security.core.annotations.PreAuthenticated;
import cn.csc.module.shop.controller.app.user.vo.AppShopUserSimpleRespVO;
import cn.csc.module.shop.convert.user.ShopUserConvert;
import cn.csc.module.shop.dal.dataobject.user.ShopUserDO;
import cn.csc.module.shop.service.user.ShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author liuchuang
 * @version 1.0
 * @date 2024/2/29 11:35
 * @modify
 */
@Api(tags = "app后台 - 店铺与用户关联关系")
@RestController
@RequestMapping("/shop/user")
@Validated
public class AppShopUserController {

    @Resource
    private ShopUserService shopUserService;


    @GetMapping("/getCurrent")
    @ApiOperation("获得当前用户的店铺与用户关联关系")
    @PreAuthenticated
    public CommonResult<AppShopUserSimpleRespVO> getCurrent() {
        ShopUserDO userDO = shopUserService.getByUserId(BigInteger.valueOf(getLoginUserId()));
        return success(ShopUserConvert.INSTANCE.convert3(userDO));
    }

    @PostMapping("/relate")
    @ApiOperation("店铺关联店铺用户(当前用户已经关联店铺)")
    @PreAuthenticated
    public CommonResult<Boolean> relate(@RequestParam BigInteger shopUserId) {
        return success(shopUserService.relate(BigInteger.valueOf(getLoginUserId()), shopUserId));
    }

    @PostMapping("/relateByShopId")
    @ApiOperation("店铺关联店铺用户(当前用户未关联店铺)")
    @PreAuthenticated
    public CommonResult<Boolean> relateByShopId(@RequestParam BigInteger shopId) {
        return success(shopUserService.relateByShopId(BigInteger.valueOf(getLoginUserId()), shopId));
    }

    @GetMapping("/shopUserList")
    @ApiOperation("获得当前用户的店铺的店铺用户列表")
    @PreAuthenticated
    public CommonResult<List<AppShopUserSimpleRespVO>> getCurrentShopUsers() {
        List<ShopUserDO> byShopId = shopUserService.getByShopId(BigInteger.valueOf(getLoginUserId()));
        return success(ShopUserConvert.INSTANCE.convertList2(byShopId));
    }


}
